<?php
require_once 'header.php';

// 获取商品ID
$product_id = isset($_GET['id']) ? (int)$_GET['id'] : 0;

// 如果没有指定商品ID，重定向回商品列表
if ($product_id <= 0) {
    header('Location: products.php');
    exit;
}

// 获取商品详情
$product = getProduct($product_id);

// 如果商品不存在，显示错误消息
if (!$product) {
    $_SESSION['flash_message'] = '商品不存在';
    $_SESSION['flash_message_type'] = 'danger';
    header('Location: products.php');
    exit;
}
?>

<div class="container">
    <nav aria-label="breadcrumb">
        <ol class="breadcrumb bg-light">
            <li class="breadcrumb-item"><a href="index.php">首页</a></li>
            <li class="breadcrumb-item"><a href="products.php">电影票</a></li>
            <li class="breadcrumb-item active" aria-current="page"><?php echo htmlspecialchars($product['name']); ?></li>
        </ol>
    </nav>

    <div class="row">
        <div class="col-md-6">
            <img src="<?php echo !empty($product['image']) ? $product['image'] : 'https://via.placeholder.com/500x400?text=电影票'; ?>" class="img-fluid rounded" alt="<?php echo htmlspecialchars($product['name']); ?>">
        </div>
        <div class="col-md-6">
            <h2><?php echo htmlspecialchars($product['name']); ?></h2>
            <p class="text-muted">商品编号: <?php echo $product['id']; ?></p>
            <hr>
            <p><?php echo nl2br(htmlspecialchars($product['description'])); ?></p>
            <div class="my-4">
                <h3 class="text-danger">¥<?php echo number_format($product['price'], 2); ?></h3>
            </div>
            <form action="add_to_cart.php" method="post">
                <input type="hidden" name="product_id" value="<?php echo $product['id']; ?>">
                <div class="form-group">
                    <label for="quantity">数量</label>
                    <div class="input-group">
                        <div class="input-group-prepend">
                            <button type="button" class="btn btn-outline-secondary" onclick="decrementQuantity()">-</button>
                        </div>
                        <input type="number" class="form-control text-center" id="quantity" name="quantity" value="1" min="1" max="10">
                        <div class="input-group-append">
                            <button type="button" class="btn btn-outline-secondary" onclick="incrementQuantity()">+</button>
                        </div>
                    </div>
                </div>
                <button type="submit" class="btn btn-primary btn-lg btn-block">
                    <i class="fas fa-shopping-cart mr-2"></i>加入购物车
                </button>
            </form>
            <div class="mt-4">
                <div class="card">
                    <div class="card-header">
                        <i class="fas fa-info-circle mr-2"></i>商品信息
                    </div>
                    <div class="card-body">
                        <p class="card-text"><i class="fas fa-check-circle text-success mr-2"></i>正品保障，正规影院电影票</p>
                        <p class="card-text"><i class="fas fa-truck text-primary mr-2"></i>电子票，购买后即可收到取票码</p>
                        <p class="card-text"><i class="fas fa-undo text-warning mr-2"></i>支持退换，以影院规定为准</p>
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>

<script>
function decrementQuantity() {
    var input = document.getElementById('quantity');
    var value = parseInt(input.value, 10);
    if (value > 1) {
        input.value = value - 1;
    }
}

function incrementQuantity() {
    var input = document.getElementById('quantity');
    var value = parseInt(input.value, 10);
    if (value < 10) {
        input.value = value + 1;
    }
}
</script>

<?php require_once 'footer.php'; ?> 